#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# @Author  : Tina Yu
# @Time    : 2021-12-25 15:37
from appium.webdriver import WebElement
from selenium.common.exceptions import NoSuchElementException


def swipe_find(driver, num, locator, duration) -> WebElement:
    """
    滑动查找元素
    :param driver: 查找的driver
    :param num: 滑动次数
    :param locator: 定位器
    :param duration: 查找时长
    :return:
    """
    for i in range(num):
        try:
            ele = driver.find_element(*locator)
            return ele
        except NoSuchElementException:
            size = driver.get_window_size()
            width = size.get("width")
            height = size.get("height")
            start_x = width / 2
            start_y = height * 0.8
            end_x = start_x
            end_y = height * 0.2
            driver.swipe(start_x, start_y, end_x, end_y, duration)
        if i == num - 1:
            raise NoSuchElementException(f"查找了{num}次，未找到元素！")
